#include <RenderWindow.h>
#include <ResourcePool.h>
#include <Mesh.h>
#include <ResourcePtr.h>
#include <d3d11.h>
#include <d3dcompiler.h>

#include <Mesh.h>
#include <Vertex.h>


using namespace std;
using namespace Display;
using namespace DirectX;

int main()
{
	Display::RenderWindow * window = Display::RenderWindow::Create(L"Game",500,500,200,200);
	
	window->SetBGColor(0.0f,0.0f,0.0f);

	ID3D11Device * device = window->GetDevice();
	ID3D11DeviceContext * context = window->GetContext();

	Display::ResourcePool * pool = Display::ResourcePool::Create(device);

	ResourcePtr * ptr = pool->AllocateMesh("mesh1");
	Mesh * mesh = (Mesh*)ptr->Get();

	Vertex_PosColor * verts = new Vertex_PosColor[3];

	verts[0].position = XMFLOAT3(-1.0f,-1.0f,0.0f);
	verts[0].color = XMFLOAT4(1.0f,0.0f,0.0,1.0f);

	verts[1].position = XMFLOAT3(0.0f,1.0f,0.0f);
	verts[1].color = XMFLOAT4(1.0f,0.0f,0.0,1.0f);

	verts[2].position = XMFLOAT3(1.0f,-1.0f,0.0f);
	verts[2].color = XMFLOAT4(1.0f,0.0f,0.0,1.0f);

	mesh->AllocateAndFillVertexBuffer(3 * sizeof(Vertex_PosColor),sizeof(Vertex_PosColor),3,0,STATIC,verts);

	ID3D11VertexShader * vertShader;
	ID3D11PixelShader * pixelShader;

	ID3DBlob * vertBuffer;
	
	D3DCompileFromFile(L"vertShader.hlsl",nullptr,nullptr,"vertStart","vs_5_0",
		D3DCOMPILE_ENABLE_STRICTNESS,0,&vertBuffer,nullptr);

	ID3D10Blob * pixelBuffer;

	HRESULT hr = D3DCompileFromFile(L"pixelShader.hlsl",nullptr,nullptr,"pixelStart","ps_5_0",
		D3DCOMPILE_ENABLE_STRICTNESS,0,&pixelBuffer,nullptr);


	device->CreateVertexShader(vertBuffer->GetBufferPointer(),vertBuffer->GetBufferSize(),nullptr,&vertShader);
	device->CreatePixelShader(pixelBuffer->GetBufferPointer(),pixelBuffer->GetBufferSize(),nullptr,&pixelShader);

	ID3D11InputLayout * layout;

	D3D11_INPUT_ELEMENT_DESC polygonLayout[2];

	polygonLayout[0].SemanticName = "POSITION";
	polygonLayout[0].SemanticIndex = 0;
	polygonLayout[0].Format = DXGI_FORMAT_R32G32B32_FLOAT;
	polygonLayout[0].InputSlot = 0;
	polygonLayout[0].AlignedByteOffset = 0;
	polygonLayout[0].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
	polygonLayout[0].InstanceDataStepRate = 0;

	polygonLayout[1].SemanticName = "COLOR";
	polygonLayout[1].SemanticIndex = 0;
	polygonLayout[1].Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
	polygonLayout[1].InputSlot = 0;
	polygonLayout[1].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
	polygonLayout[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
	polygonLayout[1].InstanceDataStepRate = 0;

	device->CreateInputLayout(polygonLayout,2,vertBuffer->GetBufferPointer(),vertBuffer->GetBufferSize(),&layout);



	while(window->IsClosed() == false)
	{
		window->HandleMessages();

		window->BeginDraw();

		context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
		context->IASetInputLayout(layout);
		context->VSSetShader(vertShader,nullptr,0);
		context->PSSetShader(pixelShader,nullptr,0);

		mesh->Draw();

		window->EndDraw();

	}


	Display::ResourcePool::Destroy(pool);
	Display::RenderWindow::Destroy(window);
	return 0;
}